var handle = {
  /**
   * 数字前置补零
   * @param  {Number}  num     传入的数字
   * @param  {Number}  length  需要的长度。默认2位
   * @return {Number}  num     转换好的数字
   */
  digit: function({ num, length = 2 }) {
    var str = ""
    num = String(num)
    length = length || 2
    for (var i = num.length; i < length; i++) {
      str += "0"
    }
    return num < Math.pow(10, length) ? str + (num | 0) : num
  },

  /**
   * 打开小程序设置，重新设置授权
   * @param {Object} modal 授权弹窗的title和内容
   * @param {function} callback 回调函数，非必填
   */
  setting: function(modal = {}, callback) {
    wx.showModal({
      title: modal.title || "请授权",
      content: modal.content || "使用该功能需要您授权允许对应的权限",
      showCancel: true,
      cancelText: "取消",
      cancelColor: "#000000",
      confirmText: "去设置",
      confirmColor: "#3CC51F",
      success: result => {
        if (result.confirm) {
          wx.openSetting({
            success(setting) {
              callback && callback(setting)
            }
          })
        }
      }
    })
  },

  /**
   * 公共弹窗
   * @param {String}  msg  弹窗消息 必填
   * @param {String}  icon  弹窗icon。[ none | success | loading ]
   * @param {number}  time  弹窗持续时间
   * @param {Boolean}  mask  是否禁止穿透
   */
  toast: function({ msg = "", icon = "none", time = 2000, mask = false }) {
    return new Promise(resolve => {
      // 处理icon问题。超出7个字后隐藏
      msg.length > 7 && icon != "none" ? (icon = "none") : ""
      // if(icon != 'none')
      wx.showToast({
        title: msg,
        icon: icon,
        duration: time,
        mask: mask,
        complete: () => setTimeout(() => resolve(), time)
      })
    })
  }
}

module.exports = handle
